import {useUserStoreExternal} from "../store/useUserStore"

let userStore = useUserStoreExternal()

function registPermission(app: any) {
    // 判断是否含有指定权限,若没有,则不显示绑定元素,使用方式:
    // v-permission="'a1,a2'":表示必须含有a1和a2权限才会显示绑定元素
    app.directive("permission", {
        mounted(el, binding) {
            const value: string = binding.value
            if (!value) {
                return
            }
            let backAuthorizedUris = userStore.userInfo.backAuthorizedUris
            if (!backAuthorizedUris || backAuthorizedUris.length === 0) {
                // 没有任何权限则移除元素
                el.parentNode && el.parentNode.removeChild(el)
            } else {
                let needAuthorities = value.split(",")
                for (let index = 0; index < needAuthorities.length; index++) {
                    const element = needAuthorities[index];
                    let isInclude = backAuthorizedUris.includes(element)
                    if (!isInclude) {
                        // 不包含
                        el.parentNode && el.parentNode.removeChild(el)
                        break
                    }
                }
            }
        }
    })
}


export default function (app: any) {
    registPermission(app)
}